using System;
using CodePlex.TfsLibrary.ClientEngine;
using CodePlex.TfsLibrary.ObjectModel;

namespace CodePlex.CodePlexClientLibrary
{
    public class CommitGuiView : StatusGuiView, ICommitView
    {
        readonly string basePath;
        readonly CommitForm form;
        readonly CommitViewPresenter presenter;

        public CommitGuiView(ITfsEngine engine,
                             string message,
                             string localPath,
                             string diffTool,
                             string diffArgs,
                             string mergeTool,
                             string mergeArgs)
            : base(engine, localPath, false, true, false, diffTool, diffArgs, mergeTool, mergeArgs)
        {
            basePath = localPath;

            form = new CommitForm();
            form.Message = message;

            presenter = new CommitViewPresenter(this, engine, diffTool, diffArgs, mergeTool, mergeArgs);
        }

        public string Message
        {
            get { return form.Message; }
        }

        public string TfsServerPath
        {
            set { form.TfsServerPath = value; }
        }

        public string TfsServerUrl
        {
            set { form.TfsServerUrl = value; }
        }

        protected override IStatusForm CreateForm()
        {
            return form;
        }

        protected override StatusViewPresenter CreatePresenter()
        {
            return presenter;
        }

        public override void DisplayNoItemsMessage()
        {
            Console.WriteLine("Nothing to commit.");
        }

        public void ShowComplete(int changesetId)
        {
            Console.WriteLine("Committed change set {0}.", changesetId);
        }

        public void ShowItemStatus(SourceItem item,
                                   SourceItemResult result)
        {
            if (result != SourceItemResult.S_Ok)
                ConsoleUtil.PrintCommandError(item.LocalName, result);
            else
                Console.WriteLine("{0}: {1}",
                                  item.LocalItemStatus == SourceItemStatus.Delete ? "Deleting" : "Uploading",
                                  CodePlexClientUtil.GetRelativePath(basePath, item.LocalName));
        }
    }
}